<template>
  <div class="home">
    <!-- 搜索 -->
    <Search></Search>

    <!-- 轮播图 -->
    <div class="banner">
      <van-swipe :autoplay="3000">
        <van-swipe-item v-for="(item, index) in banner" :key="index">
          <img :src="item.image_url" class="banner-item" />
        </van-swipe-item>
      </van-swipe>
    </div>

    <!-- 宫格 -->
    <div>
      <van-grid :column-num="channel.length">
        <van-grid-item v-for="item in channel" :key="item.id" :icon="item.icon_url" :text="item.name" />
      </van-grid>
    </div>

    <!-- 直销 -->
    <div class="box1">
      <div class="box-title">品牌制造商直供</div>
      <ul class="channel">
        <li v-for="item in brandList" :key="item.id">
          <h3>{{ item.name }}</h3>
          <p class="price">{{ item.floor_price | RMB }}</p>
          <img :src="item.pic_url" alt="">
        </li>
      </ul>
    </div>

    <!-- 新产品 -->
    <div class="box2">
      <div class="box-title">周一周四 · 新品首发</div>
      <goods-cart :list="newGoodsList"></goods-cart>
    </div>

    <!-- 推荐商品 -->
    <div class="box3">
      <div class="box-title">人气推荐</div>
      <div>
        <van-card v-for="item in hotGoodsList" :key="item.id" :price="item.retail_price | TWO" :desc="item.goods_brief"
          :title="item.name" :thumb="item.list_pic_url" />
      </div>
    </div>

    <!-- 专题精选 -->
    <div class="box4">
      <div class="box-title">专题精选</div>
      <van-swipe :loop="false" width="100vh" :show-indicators="false">
        <van-swipe-item v-for="item in topicList" :key="item.id">
          <img :src="item.item_pic_url" alt="">
          <div class="cont">
            <h3>{{ item.title }} <span>{{ item.price_info | RMB }}</span></h3>
            <p>{{ item.subtitle }}</p>
          </div>
        </van-swipe-item>
      </van-swipe>
    </div>

    <!-- 其他 -->
    <div class="more" v-for="item in categoryList" :key="item.id">
      <div class="box-title">{{ item.name }}</div>
      <goods-cart :list="item.goodsList"></goods-cart>
    </div>

    <!-- 加载动画 -->
    <Location :show="show"></Location>
  </div>
</template>

<script>
import { getHome } from '@/api/home.js';
import Search from '@/components/Search';
import Location from '@/components/Location'
import goodsCart from '@/components/goodsCart'
export default {
  data() {
    return {
      show: true,
      banner: [],
      brandList: [],
      channel: [],
      newGoodsList: [],
      hotGoodsList: [],
      topicList: [],
      categoryList: [],
    }
  },
  components: {
    Search,
    Location,
    goodsCart,
  },
  async beforeCreate() {
    let res = (await getHome());
    this.show = false;
    let data = res.data.data
    this.banner = data.banner
    this.brandList = data.brandList
    this.channel = data.channel
    this.newGoodsList = data.newGoodsList
    this.hotGoodsList = data.hotGoodsList
    this.topicList = data.topicList
    this.categoryList = data.categoryList
  },

}
</script>

<style lang="less" scoped>
.home {
  position: relative;
  margin-bottom: 50px;

  .banner {
    width: 100%;
    height: 208px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .box1 {
    width: 100%;
    position: relative;

    .channel {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;

      li {
        width: 49.5%;
        position: relative;

        h3 {
          font-size: 16px;
          font-weight: normal;
          position: absolute;
          left: 5%;
          top: 10px;
          margin: 0;
        }

        .price {
          position: absolute;
          left: 5%;
          top: 30px;
          color: #666;
          font-size: 14px;
        }

        img {
          width: 100%;
        }

      }
    }

  }

  .box2 {
    width: 100%;
    background-color: #fff;
  }

  .box3 {
    .van-card__price {
      color: red;
    }
  }

  .box4 {
    img {
      width: 100% !important;
      height: 193px;
      font-size: 0;
      display: block;
    }

    .cont {
      font-size: 16px;
      padding-left: 10px;
      background-color: #fff;

      h3 {
        padding-top: 10px;
        font-size: 16px;

        span {
          color: red;
        }
      }

      p {
        height: 50px;
        line-height: 50px;
        font-size: 14px;
        color: #3c3c3c;
      }
    }
  }

  .box-title {
    height: 50px;
    font-size: 16px;
    text-align: center;
    line-height: 50px;
    box-sizing: border-box;
    background-color: #fff;
    margin-top: 10px;
    border-top: 2px solid #f4f4f4;
  }

}
</style>